﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using StudentManagementSystem.Dtos;
using StudentManagementSystem.Entities;
using StudentManagementSystem.Helpers;
using StudentManagementSystem.Services;

[ApiController]
[Route("api/Companies")]
public class CompaniesController : ControllerBase
{
    private readonly IMapper _mapper;
    public ICompanyRepository _companyRepository { get; }

    public CompaniesController(ICompanyRepository companyRepository, IMapper mapper)
    {
        _companyRepository = companyRepository ?? throw new ArgumentNullException(nameof(companyRepository));
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
    }

    [HttpGet]
    [HttpHead]
    public async Task<ActionResult<IEnumerable<CompanyDto>>> GetCompanies(
        [FromQuery] CompanyDtoParameters parameter)
    {
        var companies = await _companyRepository.GetCompaniesAsync(parameter);
        var companyDtos = _mapper.Map<IEnumerable<CompanyDto>>(companies);
        return Ok(companyDtos);
    }

    [HttpGet("{companyId}", Name = nameof(GetCompany))]
    public async Task<ActionResult<CompanyDto>> GetCompany(Guid companyId) // 修复返回类型
    {
        var company = await _companyRepository.GetCompanyAsync(companyId);
        if (company == null)
        {
            return NotFound();
        }
        var companyDto = _mapper.Map<CompanyDto>(company);
        return Ok(companyDto);
    }

    [HttpPost]
    public async Task<ActionResult<CompanyDto>> CreateCompany(CompanyAddDto company)
    {
        var entity = _mapper.Map<Company>(company);
        _companyRepository.AddCompany(entity);
        await _companyRepository.SaveAsync();
        var returnDto = _mapper.Map<CompanyDto>(entity);
        return CreatedAtRoute(nameof(GetCompany), new { companyId = returnDto.Id }, returnDto);
    }

    [HttpPost("collection")] // 修复：添加子路径
    public async Task<ActionResult<IEnumerable<CompanyDto>>> CreateCompanyCollection(
        IEnumerable<CompanyAddDto> companyCollection)
    {
        var entities = _mapper.Map<IEnumerable<Company>>(companyCollection);
        foreach (var company in entities)
        {
            _companyRepository.AddCompany(company);
        }
        await _companyRepository.SaveAsync();
        var returnDtos = _mapper.Map<IEnumerable<CompanyDto>>(entities);
        var idsToString = string.Join(", ", returnDtos.Select(x => x.Id));
        return CreatedAtRoute(nameof(CompanyCollection), new { ids = idsToString }, returnDtos);
    }

    [HttpGet("by-ids", Name = nameof(CompanyCollection))] // 修复：固定路径
    public async Task<ActionResult<IEnumerable<CompanyDto>>> CompanyCollection(
        [FromQuery]
        [ModelBinder(BinderType = typeof(ArrayModelBinder))]
        IEnumerable<Guid> ids)
    {
        if (ids == null)
        {
            return BadRequest();
        }
        var entities = await _companyRepository.GetCompaniesAsync(ids);
        if (ids.Count() != entities.Count())
        {
            return NotFound();
        }
        var dtosToReturn = _mapper.Map<IEnumerable<CompanyDto>>(entities);
        return Ok(dtosToReturn);
    }
}